package com.hanbang.gycatering.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.text.DecimalFormat;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class SystemTools {
	/*
	 * 设置按钮点击后的渲染（Tint）
	 */
	public static void setButtonClickTint(final ImageView view,
			final int colorUp, final int colorDown) {
		view.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					view.setColorFilter(view.getResources().getColor(colorUp));
					break;
				case MotionEvent.ACTION_DOWN:
					view.setColorFilter(view.getResources().getColor(colorDown));
					break;
				case MotionEvent.ACTION_CANCEL:
					view.setColorFilter(view.getResources().getColor(colorUp));
					break;
				default:
					break;
				}

				return false;
			}
		});
	}

	/*
	 * 设置ImageView渲染（Tint）
	 */
	public static void setImageViewTint(final ImageView view, final int color) {
		view.setColorFilter(view.getResources().getColor(color));
	}

	/**
	 * 小数 四舍五入 19.0->19.0
	 * 
	 * @param val
	 * @param precision
	 * @return
	 */

	public static Double roundDouble(double val, int precision) {
		Double ret = null;
		try {
			double factor = Math.pow(10, precision);
			ret = Math.floor(val * factor + 0.5) / factor;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 小数 四舍五入 19.0->19.0
	 * 
	 * @param val
	 * @param precision
	 * @return
	 */

	public static String roundString(double val, int precision) {
		try {
			StringBuffer p = new StringBuffer(".");
			for (int i = 0; i < precision; i++) {
				p.append("0");
			}
			DecimalFormat decimalFormat = new DecimalFormat(p.toString());// 构造方法的字符格式这里如果小数不足2位,会以0补足.
			String res = decimalFormat.format(val);
			if (res.startsWith(".")) {
				res = "0" + res;
			}
			return res;// format 返回的是字符串

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/*
	 * 对网络资源文件判断路径 如果是已http开头的就返回这个值 否则在前面加上ORG
	 */
	public static String getHttpFileUrl(String url) {
		if (url != null) {
			if (url.startsWith("http://")) {
				return url;
			} else if (url.startsWith("/")) {
				return HttpInterfaces.ORG + url;
			} else {
				return HttpInterfaces.ORG + "/" + url;
			}
		}
		return url;
	}

	// 获取ApiKey
	public static String getMetaValue(Context context, String metaKey) {
		Bundle metaData = null;
		String apiKey = null;
		if (context == null || metaKey == null) {
			return null;
		}
		try {
			ApplicationInfo ai = context.getPackageManager()
					.getApplicationInfo(context.getPackageName(),
							PackageManager.GET_META_DATA);
			if (null != ai) {
				metaData = ai.metaData;
			}
			if (null != metaData) {
				apiKey = metaData.getString(metaKey);
			}
		} catch (NameNotFoundException e) {

		}
		return apiKey;
	}

	public static String readInputStream(InputStream is) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			byte[] buffer = new byte[1024];
			while ((len = is.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			is.close();
			baos.close();
			byte[] result = baos.toByteArray();
			return new String(result, "GB2312");
		} catch (Exception e) {
			e.printStackTrace();
			return "获取失败";
		}

	}

	public static String readInputStream(InputStream is, String coding) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int len = 0;
			byte[] buffer = new byte[1024];
			while ((len = is.read(buffer)) != -1) {
				baos.write(buffer, 0, len);
			}
			is.close();
			baos.close();
			byte[] result = baos.toByteArray();
			return new String(result, coding);
		} catch (Exception e) {
			e.printStackTrace();
			return "获取失败";
		}

	}

	/**
	 * 将px值转换为dip或dp值，保证尺寸大小不变
	 * 
	 * @param pxValue
	 * @param scale
	 *            （DisplayMetrics类中属�?�density�?
	 * @return
	 */
	public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	/**
	 * 将dip或dp值转换为px值，保证尺寸大小不变
	 * 
	 * @param dipValue
	 * @param scale
	 *            （DisplayMetrics类中属�?�density�?
	 * @return
	 */
	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	/**
	 * 将px值转换为sp值，保证文字大小不变
	 * 
	 * @param pxValue
	 * @param fontScale
	 *            （DisplayMetrics类中属�?�scaledDensity�?
	 * @return
	 */
	public static int px2sp(Context context, float pxValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (pxValue / fontScale + 0.5f);
	}

	/**
	 * 将sp值转换为px值，保证文字大小不变
	 * 
	 * @param spValue
	 * @param fontScale
	 *            （DisplayMetrics类中属�?�scaledDensity�?
	 * @return
	 */
	public static int sp2px(Context context, float spValue) {
		final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
		return (int) (spValue * fontScale + 0.5f);
	}

	/**
	 * 2 * 获取版本�? 3 * @return 当前应用的版本号 4
	 */
	public static String getVersionName(Context context) {
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo info = manager.getPackageInfo(context.getPackageName(),
					0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	/**
	 * 2 * 获取版本ma
	 */
	public static int getVersionCode(Context context) {
		try {
			PackageManager manager = context.getPackageManager();
			PackageInfo info = manager.getPackageInfo(context.getPackageName(),
					0);
			int version = info.versionCode;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
}
